package demo06;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class DouDiZhu {
    public static void main(String[] args) {
        HashMap<Integer, String> poker = prepareCards(); // 备牌
        List<Integer> num = shuffleCards(poker); // 洗牌
        List<List<String>> players = dealCards(num, poker); // 发牌
        WatchCards(players); // 看牌
    }

    private static void WatchCards(List<List<String>> players) {
        System.out.println("player1：" + players.get(0)); // 集合下标第0号为玩家1的牌
        System.out.println("player2：" + players.get(1)); // 集合下标第1号为玩家2的牌
        System.out.println("player3：" + players.get(2)); // 集合下标第2号为玩家3的牌
        System.out.println("diPai：" + players.get(3)); // 集合下标第3号为底牌
    }

    private static List<List<String>> dealCards(List<Integer> num, HashMap<Integer, String> poker) {
        List<Integer> player1Num = new ArrayList<>();
        List<Integer> player2Num = new ArrayList<>();
        List<Integer> player3Num = new ArrayList<>();
        List<Integer> diPaiNum = new ArrayList<>();
        for (Integer n : num) { // num是个随机数组，将随机数编号分别发给每个玩家和底牌
            if (n > num.size() - 4) { // 剩余牌只剩3张时发给底牌
                diPaiNum.add(num.get(n));
            } else if (n % 3 == 0) { // n%3有三个结果，分别代表给三个玩家发牌
                player1Num.add(num.get(n));
            } else if (n % 3 == 1) {
                player2Num.add(num.get(n));
            } else if (n % 3 == 2) {
                player3Num.add(num.get(n));
            }
        }
        Collections.sort(player1Num); // 理牌，先将每个编号数字按顺序排列
        Collections.sort(player2Num);
        Collections.sort(player3Num);
        Collections.sort(diPaiNum);

        List<String> player1 = new ArrayList<>();
        List<String> player2 = new ArrayList<>();
        List<String> player3 = new ArrayList<>();
        List<String> diPai = new ArrayList<>();
        List<List<String>> allCards = new ArrayList<>();
        for (Integer integer : player1Num) { // 根据已经排序好的编号数组，找到map扑克集合中编号键对应的每个值，放入新字符串数组
            player1.add(poker.get(integer));
        }
        for (Integer integer : player2Num) {
            player2.add(poker.get(integer));
        }
        for (Integer integer : player3Num) {
            player3.add(poker.get(integer));
        }
        for (Integer integer : diPaiNum) {
            diPai.add(poker.get(integer));
        }
        Collections.addAll(allCards, player1, player2, player3, diPai); // 整理好存放进一个大集合中，便于输出

        return allCards;
    }

    private static List<Integer> shuffleCards(HashMap<Integer, String> poker) { // 洗牌：理出并打乱扑克的编号
        Set<Integer> key = poker.keySet(); // 拉出扑克的键集合，即编号集合
        List<Integer> num = new ArrayList<>();
        for (Integer k : key) {
            num.add(k); // 整理编号到List集合，使其可以使用Collections下的shuffle方法
        }
        Collections.shuffle(num); // 打乱编号
        System.out.println(num); // [42, 3, 7, 46, 8, 23, ...]

        return num;
    }

    private static HashMap<Integer, String> prepareCards() { // 备牌：准备一副洗好的扑克
        HashMap<Integer, String> poker = new HashMap<>();
        List<String> color = new ArrayList<>();
        List<String> num = new ArrayList<>();
        List<Integer> key = new ArrayList<>();
        List<String> value = new ArrayList<>();
        Collections.addAll(color, "黑桃", "红桃", "梅花", "方片");
        Collections.addAll(num, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        value.add("大王");
        value.add("小王");
        for (String n : num) {
            for (String c : color) {
                value.add(c + n); // value：编入每张扑克的名字
            }
        }
        for (int i = 0; i < value.size(); i++) { // 依照扑克张书获得编号数量
            key.add(i); // key：编入扑克的编号
        }
        for (int i = 0; i < value.size(); i++) {
            poker.put(key.get(i), value.get(i)); // 键使用扑克的编号，值使用扑克的名字
        }
        System.out.println(poker); // {0=大王, 1=小王, 2=黑桃2, 3=红桃2, ..., 53=方片3}

        return poker;
    }
}